/**
 * 
 */

var oauth=ChromeExOAuth.fromConfig({
			'request_url': "http://api.t.sina.com.cn/oauth/request_token",
			'authorize_url': "http://api.t.sina.com.cn/oauth/authorize",
			'access_url': "http://api.t.sina.com.cn/oauth/access_token",
			'consumer_key':"3640942651",
			'consumer_secret': "2d5ce5527431c7ee3670f94de918d248",
			'scope':'',
			'app_name': "i新浪微博"
	});

function getuserinfo(){
	 var url = 'http://api.t.sina.com.cn/users/show.json';
	 var userid=oauth.getUserId();
	  var request = {
	    'method': 'post',
	    'parameters':{
	    	'user_id':userid
	    }
	  };

	  oauth.sendSignedRequest(url, displayuserinfo, request);
}

function encodejson(str){
	return eval("("+str+")");
}


function displayuserinfo(resp,xhr){
	hide('nouser');
	show('userinfo');
	fill(encodejson(resp));
}

function show(id){
	var d=document.getElementById(id);
	d.className="area_on";
}

function hide(id){
	var d=document.getElementById(id);
	d.className="area_off";
}

function recommend(){
	if(!oauth.hasToken()){
		return;
	}
	
	var url = 'http://api.t.sina.com.cn/statuses/update.json';
	  var encode_status=encodeURIComponent("用chrome玩微博，一定要试试一键微博。使用chrome的右键菜单实现一键发送微博。 https://chrome.google.com/extensions/detail/dlnibcgilcbfohdalmandfokomeljkfc");
	  var request = {
	    'method': 'post',
	    'parameters':{
			'status':encode_status
		    }
	  };

	  // Send: GET https://docs.google.com/feeds/default/private/full?alt=json
	  oauth.sendSignedRequest(url, function(resp,xhr){
		return resp;
	  }, request);
}


function login(){
	oauth.authorize(function(){
		getuserinfo();
	});
}

function logout(){
	oauth.clearTokens();
	hide('userinfo');
	show('nouser');
}
